#include "mkqt.h"

void convert(int argc, char **argv)
{
    // Check if amount of command line arguments is correct
    // First parameter is the execuable file itself
    // Second is the path to the image files
    // Third is the column count used to place images in an 2D array
    if(argc<3)
    {
        wxPrintf(wxT("Invalid amount of arguments\nPress Enter to exit"));
        wxScanf(new wxChar);
        exit(1);
    }
    // Convert second parameter argv[1] from char* to wxString
    wxString imgPath(argv[1], wxConvUTF8);
    // Check if path is a valid directory
    if(!wxDirExists(imgPath))
    {
        wxPrintf(wxT("Second argument is not a valid directory\nPress Enter to exit"));
        wxScanf(new wxChar);
        exit(2);
    }
    // Convert third parameter argv[2] from char* to wxString to int
    wxUint32 imgRowCount = wxAtoi(wxString(argv[2], wxConvUTF8));
    // Check if third parameter is a valid integer
    if(imgRowCount == 0)
    {
        wxPrintf(wxT("Third argument is not a valid integer\nPress Enter to exit"));
        wxScanf(new wxChar);
        exit(3);
    }

    makeQT(imgRowCount, imgPath, imgPath+wxT("\\output.mov"));
}

void makeQT(wxUint32 imgRowCount, wxString inputDirectory, wxString outputFile)
{
    wxPrintf(wxT("Scanning Directory:\n") + inputDirectory + wxT("\n\n"));

    wxArrayString files;
    wxDirTraverserFiles fileTraverser(files);
    wxDir(inputDirectory).Traverse(fileTraverser, wxT(""), wxDIR_DEFAULT);

    // support only for png, bmp, jpg, gif, tif, tga, xpm
    // remove all other files from the file list
    for(wxUint32 i=0; i<files.Count(); i++)
    {
        if(!files[i].EndsWith(wxT(".png")) && !files[i].EndsWith(wxT(".bmp")) && !files[i].EndsWith(wxT(".jpg")) && !files[i].EndsWith(wxT(".gif")) && !files[i].EndsWith(wxT(".tif")) && !files[i].EndsWith(wxT(".tga")) && !files[i].EndsWith(wxT(".xpm")))
        {
            files.RemoveAt(i);
            i--;
        }
    }

    wxUint32 imgCount = files.Count();

    if(imgCount == 0)
    {
        wxPrintf(wxT("No supported images found in the given directory (png|bmp|jpg|gif|tif|tga|xpm)\nPress Enter to exit"));
        wxScanf(new wxChar);
        exit(4);
    }

    wxUint32 imgColumnCount = imgCount/imgRowCount;

    if(imgCount<imgColumnCount || imgCount%imgColumnCount!=0)
    {
        wxPrintf(wxT("Third argument is not a valid divisor for the number of images in the given directory\nPress Enter to exit"));
        wxScanf(new wxChar);
        exit(5);
    }

    wxInitAllImageHandlers();

    wxImage images[imgCount];

    for(wxUint32 i=0; i<imgCount; i++)
    {
        wxPrintf(wxT("Loading: ") + files[i].AfterLast('\\') + wxT("\n"));
        images[i] = wxImage(files[i]);
        // define jpg quality for this image, when exporting into the qtvr movie
        images[i].SetOption(wxT("quality"), 85);
    }

    // create an QTVR instance based on the images and save this to the output file
    QT qt(images, imgCount, imgColumnCount, imgRowCount);
    qt.save(outputFile);

    wxPrintf(wxT("\nOperation Completed\nPress Enter to exit"));
    wxScanf(new wxChar);
}
